package constans;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.util.Log;
import org.newdawn.slick.util.ResourceLoader;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class ResourceManager {
	
	private static String baseDir = null;
	
	private static HashMap<String, Sound> sounds = new HashMap<String, Sound>();
	private static HashMap<String, Image> images = new HashMap<String, Image>();

	public static void loadResources(String ref) throws IOException {
		loadResources(ResourceLoader.getResourceAsStream(ref));
	}
	public static void loadResources(File ref) throws IOException {
		loadResources(new FileInputStream(ref));
	}
	public static void loadResources(InputStream ref) throws IOException {
		try {
			DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			Document document = builder.parse(ref);
	
			Element element = document.getDocumentElement();
			if (!element.getNodeName().equals("resources")) {
				throw new IOException("Not a resource configuration file");
			}
			// first look for basedir
			NodeList list = element.getElementsByTagName("basedir");
			for (int i = 0; i < list.getLength(); i++) {
				setBaseDirectory((Element) list.item(i));
			}
			// load images
			list = element.getElementsByTagName("image");
			for (int i = 0; i < list.getLength(); i++) {
				loadImage((Element) list.item(i));
			}
			list = element.getElementsByTagName("sound");
			for (int i = 0; i < list.getLength(); i++) {
				loadSound((Element) list.item(i));
			}

		} catch (IOException e) {
			Log.error(e);
			throw e;
		} catch (Exception e) {
			Log.error(e);
			throw new IOException("Unable to load resource configuration file");
		}
	}
	//ladowanie obrazka--------------------------------------------------------------
	private static void loadImage(Element img) throws SlickException {
		String key = img.getAttribute("key");
		String file = img.getAttribute("file");
			
		Image image;
		
		if (baseDir != null && !file.startsWith(baseDir)) file = baseDir + file;

		image = new Image(file);
		images.put(key, image);		
	}
	//ladowanie dzieku----------------------------------------------------------------
	private static void loadSound(Element snd) throws SlickException {
		String key = snd.getAttribute("key");
		String file = snd.getAttribute("file");

		if (baseDir != null && !file.startsWith(baseDir)) file = baseDir + file;
		Sound sound = new Sound(file);
		sounds.put(key, sound);
	}
	
	//sciezka do pliku----------------------------------------------------------------
	private static void setBaseDirectory(Element basedir) throws SlickException {
		String dir = basedir.getAttribute("path");
		setBaseDirectory(dir);
	}
	public static void setBaseDirectory(String baseDirectory)
			throws SlickException {
		Log.debug("Setting ResourceManager base directory to '" + baseDirectory
				+ "'");
		if (baseDirectory == null || baseDirectory.isEmpty())
			throw new SlickException("BaseDirectory must not be null or empty!");
		baseDir = baseDirectory;
		if (!baseDir.endsWith("/"))
			baseDir = baseDir + "/";
	}
	
	//pobieranie------------------------------------------------------------------------
	
	//pobieranie dzwieku
	public static Sound getSound(String key) {
		Sound sound = sounds.get(key);
		if (sound == null) Log.error("No sound for key " + key + " found!");
		return sound;
	}
	//pobieranie obrazka
	public static Image getImage(String key) {
		Image image = images.get(key);
		if (image == null) Log.error("No image for key " + key + " found!");
		return image;
	}

}
